package com.yufei.infoExtractor.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.zip.Inflater;

public class IOUtils {
	protected static final int BUFFER_SIZE = 1024;
	protected static final String default_encoding="utf-8";
    public static byte[] translantStreamToByte(InputStream in){
    	byte[] buffer = new byte[BUFFER_SIZE];
    	byte[] conts=null;
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		int bufferFilled;
		try {
			while ((bufferFilled = in.read(buffer, 0, buffer.length)) != -1) {
				out.write(buffer, 0, bufferFilled);
			}

			conts = out.toByteArray();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (in != null) {
					in.close();
				}
				if (out != null) {
					out.close();
				}
			} catch (IOException e) {

			}
			return conts;
		}
    }

			
		
    

	public static String  translantByteStreamToString(byte[] bytes, String encoding) {
		if(encoding==null){
			encoding=default_encoding;
		}
		String pData=null;
		    try{
			 pData = new String(bytes, Charset.forName(encoding));
		    }catch(Exception e){
		    	 pData = new String(bytes, Charset.forName(default_encoding));
		    }
		
			return pData;
		
	}
	
	/**
	 * 针对deflate压缩格式网页，进行特殊处理网页解析
	 * @param charset
	 * @param input
	 * @return
	 * @throws Exception
	 */
	public static String deflateByte2String(String charset,byte[] input)throws Exception{
		//Inflater inflater = new Inflater(true); 
		//inflater.setInput(input);
		byte[] temp=new byte[input.length*10];
		int length=input.length;
		byte[] results=new byte[length];
		System.arraycopy(temp, 0, results, 0, input.length);
		//inflater.end();
		return new String(results,charset);
	}
	public static void main(String[] args){
    

		
	}
}
